<?php

namespace App\Controllers;

class Alist extends BaseController {

    public function index() {
        // connect to the model
        $places = new \App\Models\Places();
        // retrieve all the records
        $records = $places->findAll();

        $table = new \CodeIgniter\View\Table();
        $headings = $places->fields;
        $displayHeadings = array_slice($headings, 1, 2);
        $table->setHeading(array_map('ucfirst', $displayHeadings));

        foreach ($records as $record) {
            $nameLink = anchor("Alist/showme/$record->id", $record->name);
            $table->addRow($nameLink, "<img src=/image/$record->image />",$record->Description);
            
        }
        
        $template = [
            'table_open' => '<table cellpadding="5px">',
            'cell_start' => '<td style="border: 1px solid #dddddd;">',
            'row_alt_start' => '<tr style="background-color:#dddddd">',
        ];

        $fields = [
            'title' => 'Apex Legends',
            'heading' => 'Apex Legends',
            'footer' => 'Copyright Xavier'
        ];

        $table->setTemplate($template);

        // get a template parser
        $parser = \Config\Services::parser();

        return $parser->setData($fields)
                        ->render('templates\top') .
                $table->generate() .
                        $parser->setData($fields)
                        ->render('templates\bottom');
    }

    public function showme($id) {
        // connect to the model
        $places = new \App\Models\Places();
        // retrieve all the records
        $record = $places->find($id);
        
        $table = new \CodeIgniter\View\Table();
        $headings = $places->fields;
       $table->addRow(ucfirst($headings[0]),$record['id']);
$table->addRow(ucfirst($headings[1]),$record['name']);
$table->addRow(ucfirst($headings[2]),$record['gender']);
$table->addRow(ucfirst($headings[3]),$record['height']);
$table->addRow(ucfirst($headings[4]),$record['weight']);
$table->addRow(ucfirst($headings[5]),$record['age']);
$table->addRow(ucfirst($headings[6]),$record['Home']);
$table->addRow(ucfirst($headings[7]),$record['Description']);
$table->addRow(ucfirst($headings[8]),$record['Tourism site']);
$table->addRow(ucfirst($headings[9]),$record['image']);
        
        $imgString = "<img src=\"/image/".$record['image']."\"/>";
        $nameLink = anchor("Alist/edit/" . $record['id'], "revise");
         $table->addRow($nameLink);
        $table->addRow(['data' => $imgString, 'colspan' => 2]);
        
        // get a template parser
        $parser = \Config\Services::parser();
        
        $fields = [
            'title' => 'Travel Destination',
            'heading' => $record['name'],
            'footer' => 'Copyright Xavier'
        ];
        
        // tell it about the substitions
        return $parser->setData($fields)
                        ->render('templates\top') .
                $table->generate() .
                        $parser->setData($fields)
                        ->render('templates\bottom');
    }
    public function edit($id = NULL) {
        $places = new \App\Models\Places();
$record = $places->find($id);
$fields = [
'title' => 'Edit a Destination',
'heading' => 'Edit a Destination',
'footer' => 'Copyright Xavier'
];
$parser = \Config\Services::parser();
return $parser->setData($fields)
->render('templates\top') .
$parser->setData($record)
->render('editplace') .
$parser->setData($fields)
->render('templates\bottom');
}
public function handle() {
    $places = new \App\Models\Places();
helper(['form', 'url']);
$record = $places->find($this->request->getPost('id'));
if ($this->validate($places->validationRules)) {
$places->save([
'id' => $this->request->getPost('id'),
'name' => $this->request->getPost('name'),
'gender' => $this->request->getPost('gender'),
'height' => $this->request->getPost('height'),
'weight' => $this->request->getPost('weight'),
'age' => $this->request->getPost('age'),
'Home' => $this->request->getPost('Home'),    
'Description' => $this->request->getPost('Description'),
'Tourism site' => $this->request->getPost('Tourism site'),
'image' => $this->request->getPost('image'),
]);
}
$fields = [
'title' => 'Edit a Destination',
'heading' => 'Edit a Destination',
'footer' => 'Copyright Xavier'
];
$parser = \Config\Services::parser();
//Top of page
$returnView = $parser->setData($fields)->render('templates\top');
//Body of page
if (!$this->validate($places->validationRules)) {
$returnView .= $parser->renderString($this->validator->listErrors());
$returnView .= $parser->setData($this->request->getPost())
->render('editplace');
} else {
$returnView .= $parser->renderString("<p>Success!</p>");
$returnView .= $parser->setData($record)
->render('editplace');
}
//Bottom of page
$returnView .= $parser->setData($fields)
->render('templates\bottom');
return $returnView;
}
}

